<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - compress_stream_kernel_1.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2003  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_COMPRESS_STREAM_KERNEl_1_
<font color='#0000FF'>#define</font> DLIB_COMPRESS_STREAM_KERNEl_1_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>streambuf<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cstdio<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='compress_stream_kernel_abstract.h.html'>compress_stream_kernel_abstract.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> fce,
        <font color='#0000FF'>typename</font> fcd,
        <font color='#0000FF'>typename</font> crc32
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>class</font> <b><a name='compress_stream_kernel_1'></a>compress_stream_kernel_1</b>
    <b>{</b>
        <font color='#009900'>/*!
            REQUIREMENTS ON fce
                is an implementation of entropy_encoder_model/entropy_encoder_model_kernel_abstract.h
                the alphabet_size of fce must be 257.
                fce and fcd share the same kernel number.

            REQUIREMENTS ON fcd
                is an implementation of entropy_decoder_model/entropy_decoder_model_kernel_abstract.h
                the alphabet_size of fcd must be 257.
                fce and fcd share the same kernel number.

            REQUIREMENTS ON crc32
                is an implementation of crc32/crc32_kernel_abstract.h



            INITIAL VALUE
                this object has no state

            CONVENTION
                this object has no state
        !*/</font>

        <font color='#0000FF'>const</font> <font color='#0000FF'>static</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> eof_symbol <font color='#5555FF'>=</font> <font color='#979000'>256</font>;

    <font color='#0000FF'>public</font>:

        <font color='#0000FF'>class</font> <b><a name='decompression_error'></a>decompression_error</b> : <font color='#0000FF'>public</font> dlib::error 
        <b>{</b> 
            <font color='#0000FF'>public</font>: 
                <b><a name='decompression_error'></a>decompression_error</b><font face='Lucida Console'>(</font>
                    <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> i
                <font face='Lucida Console'>)</font> :
                    dlib::error<font face='Lucida Console'>(</font>std::string<font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b><b>}</b>

                <b><a name='decompression_error'></a>decompression_error</b><font face='Lucida Console'>(</font>
                    <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> i
                <font face='Lucida Console'>)</font> :
                    dlib::error<font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>
                <b>{</b><b>}</b>
        <b>}</b>;


        <b><a name='compress_stream_kernel_1'></a>compress_stream_kernel_1</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>
        <b>{</b><b>}</b>

        ~<b><a name='compress_stream_kernel_1'></a>compress_stream_kernel_1</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>
        <b>{</b><b>}</b>

        <font color='#0000FF'><u>void</u></font> <b><a name='compress'></a>compress</b> <font face='Lucida Console'>(</font>
            std::istream<font color='#5555FF'>&amp;</font> in,
            std::ostream<font color='#5555FF'>&amp;</font> out
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

        <font color='#0000FF'><u>void</u></font> <b><a name='decompress'></a>decompress</b> <font face='Lucida Console'>(</font>
            std::istream<font color='#5555FF'>&amp;</font> in,
            std::ostream<font color='#5555FF'>&amp;</font> out
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

    <font color='#0000FF'>private</font>:

        <font color='#009900'>// restricted functions
</font>        <b><a name='compress_stream_kernel_1'></a>compress_stream_kernel_1</b><font face='Lucida Console'>(</font>compress_stream_kernel_1<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;        <font color='#009900'>// copy constructor
</font>        compress_stream_kernel_1<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font>compress_stream_kernel_1<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;    <font color='#009900'>// assignment operator
</font>
    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// member function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> fce,
        <font color='#0000FF'>typename</font> fcd,
        <font color='#0000FF'>typename</font> crc32
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> compress_stream_kernel_1<font color='#5555FF'>&lt;</font>fce,fcd,crc32<font color='#5555FF'>&gt;</font>::
    <b><a name='compress'></a>compress</b> <font face='Lucida Console'>(</font>
        std::istream<font color='#5555FF'>&amp;</font> in_,
        std::ostream<font color='#5555FF'>&amp;</font> out_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        std::streambuf::int_type temp;

        std::streambuf<font color='#5555FF'>&amp;</font> in <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>in_.<font color='#BB00BB'>rdbuf</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'>typename</font> fce::entropy_encoder_type coder;
        coder.<font color='#BB00BB'>set_stream</font><font face='Lucida Console'>(</font>out_<font face='Lucida Console'>)</font>;

        fce <font color='#BB00BB'>model</font><font face='Lucida Console'>(</font>coder<font face='Lucida Console'>)</font>;

        crc32 crc;

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// write out a known value every 20000 symbols
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>count <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>20000</font><font face='Lucida Console'>)</font>
            <b>{</b>
                count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                coder.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font><font color='#979000'>1500</font>,<font color='#979000'>1501</font>,<font color='#979000'>8000</font><font face='Lucida Console'>)</font>;
            <b>}</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>count;

            <font color='#009900'>// get the next character
</font>            temp <font color='#5555FF'>=</font> in.<font color='#BB00BB'>sbumpc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

            <font color='#009900'>// if we have hit EOF then encode the marker symbol
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>!</font><font color='#5555FF'>=</font> EOF<font face='Lucida Console'>)</font>  
            <b>{</b>
                <font color='#009900'>// encode the symbol
</font>                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                crc.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'>continue</font>;
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font>eof_symbol<font face='Lucida Console'>)</font>;

                <font color='#009900'>// now write the checksum
</font>                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> checksum <font color='#5555FF'>=</font> crc.<font color='#BB00BB'>get_checksum</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte1 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>checksum<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>24</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte2 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>checksum<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte3 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>checksum<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font><font face='Lucida Console'>)</font>;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte4 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>checksum<font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font><font face='Lucida Console'>)</font>;

                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font>byte1<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font>byte2<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font>byte3<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>encode</font><font face='Lucida Console'>(</font>byte4<font face='Lucida Console'>)</font>;

                <font color='#0000FF'>break</font>;
            <b>}</b>
        <b>}</b>      
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> fce,
        <font color='#0000FF'>typename</font> fcd,
        <font color='#0000FF'>typename</font> crc32
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> compress_stream_kernel_1<font color='#5555FF'>&lt;</font>fce,fcd,crc32<font color='#5555FF'>&gt;</font>::
    <b><a name='decompress'></a>decompress</b> <font face='Lucida Console'>(</font>
        std::istream<font color='#5555FF'>&amp;</font> in_,
        std::ostream<font color='#5555FF'>&amp;</font> out_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>

        std::streambuf<font color='#5555FF'>&amp;</font> out <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>out_.<font color='#BB00BB'>rdbuf</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'>typename</font> fcd::entropy_decoder_type coder;
        coder.<font color='#BB00BB'>set_stream</font><font face='Lucida Console'>(</font>in_<font face='Lucida Console'>)</font>;

        fcd <font color='#BB00BB'>model</font><font face='Lucida Console'>(</font>coder<font face='Lucida Console'>)</font>;

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> symbol;
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

        crc32 crc;

        <font color='#009900'>// decode until we hit the marker symbol
</font>        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// make sure this is the value we expect
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>count <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>20000</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>coder.<font color='#BB00BB'>get_target</font><font face='Lucida Console'>(</font><font color='#979000'>8000</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>1500</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>throw</font> <font color='#BB00BB'>decompression_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error detected in compressed data stream.</font>"<font face='Lucida Console'>)</font>;
                <b>}</b>
                count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                coder.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font><font color='#979000'>1500</font>,<font color='#979000'>1501</font><font face='Lucida Console'>)</font>;
            <b>}</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>count;

            <font color='#009900'>// decode the next symbol
</font>            model.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>symbol <font color='#5555FF'>!</font><font color='#5555FF'>=</font> eof_symbol<font face='Lucida Console'>)</font>
            <b>{</b>
                crc.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                <font color='#009900'>// write this symbol to out
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>out.<font color='#BB00BB'>sputc</font><font face='Lucida Console'>(</font><font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>throw</font> std::ios::<font color='#BB00BB'>failure</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>error occurred in compress_stream_kernel_1::decompress</font>"<font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>continue</font>;
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                <font color='#009900'>// we read eof from the encoded data.  now we just have to check the checksum and we are done.
</font>                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte1;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte2;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte3;
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> byte4;

                model.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>; byte1 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>; byte2 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>; byte3 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>;
                model.<font color='#BB00BB'>decode</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>; byte4 <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>symbol<font face='Lucida Console'>)</font>;

                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> checksum <font color='#5555FF'>=</font> byte1;
                checksum <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>8</font>;
                checksum <font color='#5555FF'>|</font><font color='#5555FF'>=</font> byte2;
                checksum <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>8</font>;
                checksum <font color='#5555FF'>|</font><font color='#5555FF'>=</font> byte3;
                checksum <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>8</font>;
                checksum <font color='#5555FF'>|</font><font color='#5555FF'>=</font> byte4;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>checksum <font color='#5555FF'>!</font><font color='#5555FF'>=</font> crc.<font color='#BB00BB'>get_checksum</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                    <font color='#0000FF'>throw</font> <font color='#BB00BB'>decompression_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error detected in compressed data stream.</font>"<font face='Lucida Console'>)</font>;

                <font color='#0000FF'>break</font>;
            <b>}</b>
        <b>}</b> <font color='#009900'>// while (true)
</font>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_COMPRESS_STREAM_KERNEl_1_
</font>

</pre></body></html>